import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MyTankGame extends JFrame implements ActionListener {
    String path = MyTankGame.class.getClassLoader().getResource("").getPath()+"music";

   MyPanel myPanel=null;
   // 定义一个开始面板
   MyStartPanel myStartPanel=null;
   // 菜单
   JMenuBar jMenuBar=null;
   //开始
   JMenu jMenu=null;
   // 菜单子项
   JMenuItem jMenuItemStart=null;
   JMenuItem jMenuItemOut=null;
   //保存  退出
   JMenuItem jMenuItemSave=null;
   JMenuItem jMenuItemGoOn=null;


    public static void main(String[] args) {
        MyTankGame myTankGame=new MyTankGame();


    }

    public MyTankGame()  {
        jMenuBar=new JMenuBar();
        jMenu=new JMenu("游戏（G）");
        //设置快捷方式
        jMenu.setMnemonic('G');
        jMenuItemStart=new JMenuItem("开始新游戏(N)");
        jMenuItemOut=new JMenuItem("退出游戏(E)");
        jMenuItemSave=new JMenuItem("保存退出游戏(C)");
        jMenuItemGoOn=new JMenuItem("继续上局游戏(S)");
        //  注册监听
        jMenuItemGoOn.addActionListener(this);
        jMenuItemGoOn.setActionCommand("GoOnGame");

        jMenuItemSave.addActionListener(this);
        jMenuItemSave.setActionCommand("SaveExit");

        jMenuItemOut.addActionListener(this);
        jMenuItemOut.setActionCommand("Exit");
        jMenuItemOut.setMnemonic('E');

        jMenuItemStart.addActionListener(this);
        jMenuItemStart.setActionCommand("NewGame");

        jMenu.add(jMenuItemStart);
        jMenu.add(jMenuItemOut);
        jMenu.add(jMenuItemSave);
        jMenu.add(jMenuItemGoOn);
        jMenuBar.add(jMenu);
        myStartPanel=new MyStartPanel();
        Thread thread=new Thread(myStartPanel);
        thread.start();

        this.setJMenuBar(jMenuBar);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.add(myStartPanel);
        this.setSize(600,500);
        this.setVisible(true);



    }

    @Override
    public void actionPerformed(ActionEvent e) {

        // 不同的点击不同的处理
        if(e.getActionCommand().equals("NewGame")){
            //  创建战场面板
            myPanel=new MyPanel("NewGame",path);
            Thread thread=new Thread(myPanel);
            thread.start();
            this.remove(myStartPanel);
            this.add(myPanel);
            this.addKeyListener(myPanel);

            this.setVisible(true);


        }else if(e.getActionCommand().equals("Exit")){
            // 用户点击了退出系统的菜单
            // 保存击毁敌人数量
            Recorder.keepRecording(path);
            System.exit(0);

        }else if(e.getActionCommand().equals("SaveExit")){
             Recorder recorder=new Recorder();
             recorder.setEnemyTanks(myPanel.getEnemyTanks());
             recorder.keepRecAndEnemyTank(path);
             System.exit(0);
        }else if(e.getActionCommand().equals("GoOnGame")){

            myPanel=new MyPanel("GoOnGame",path);
            Thread thread=new Thread(myPanel);
            thread.start();
            this.remove(myStartPanel);
            this.add(myPanel);
            this.addKeyListener(myPanel);

            this.setVisible(true);

        }


    }
}
